﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using WXYW.DAL;

namespace WXYW.BLL
{
    public class SMSService
    {
        public static string SendSMS(string content, string mobilePhone)
        {
            string retCode = "-3";
            try
            {
                LogUtility.LogUtilityIntance.LogMessgae(string.Format("SMS Content:{0}; Mobile Phone: {1}", content, mobilePhone));
                if (!string.IsNullOrEmpty(mobilePhone) &&
                    !string.IsNullOrEmpty(content))
                {
                    WebRequest request = WebRequest.Create(string.Format("http://172.16.1.66/sendMessage.asp?Content={0}&id={1}&ip={2}&pwd={3}&style=81",
                        en(content), mobilePhone.Trim(), "172.16.1.86", "sendpassword123"));
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded";
                    Stream dataStream = request.GetRequestStream();
                    dataStream.Close();
                    WebResponse response = request.GetResponse();
                    dataStream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(dataStream);
                    retCode = reader.ReadToEnd();
                    if (retCode == "-1")
                    {
                        UserService userService = new UserService();
                        List<User> users = userService.GetUsersByType(1);
                        foreach (User user in users)
                        {
                            EmailService.SendEmail(user.UserEmail, "短信平台超过限制通知", "本月短信平台的短信数目已用完，请申请增加短信数量。");
                        }
                    }
                    LogUtility.LogUtilityIntance.LogMessgae(string.Format("SMS Content:{0}; Mobile Phone: {1}; Status: {2}", content, mobilePhone, retCode));
                    reader.Close();
                    dataStream.Close();
                    response.Close();                    
                }
            }
            catch (Exception ex)
            {
                retCode = "-2";
                LogUtility.LogUtilityIntance.LogException(ex, string.Format("SMS Content:{0}; Mobile Phone: {1}", content, mobilePhone));
            }
            return retCode;
        }

        public static String en(string src)
        {
            string ret = "";
            foreach (char c in src.ToCharArray())
            {
                if (Encoding.Default.GetBytes(c + "").Length > 1)
                {
                    foreach (byte i in Encoding.Default.GetBytes(c + ""))
                    {
                        ret += "%" + ((int)i).ToString("X");
                    }
                }
                else
                {
                    ret += c;
                }
            }
            return ret;
        }
    }
}
